C++中定义别名的几种方式总结 您所在的位置:网站首页 python 变量别名 C++中定义别名的几种方式总结

C++中定义别名的几种方式总结

2024-07-11 15:55| 来源: 网络整理| 查看: 265

背景

在代码编辑过程中,为了书写省事或者更容易理解,通常会自定义别名,包括类型别名、方法别名等。在 C++ 中定义别名有以下几种方式。

#define

①.概述

#define 是宏定义,作用就是将一个标识符定义为一个字符串,源程序中所有的该标识符均以指定的字符串代替,在预编译阶段执行。

②.定义类型别名

#include "iostream" using namespace std; #define intPtr int * //定义一个 int * 指针类型 int main() { intPtr x = new int(6); cout PRINTHELLO(); system("pause"); return 0; }

运行结果如下:

在这里插入图片描述

typedef

①.概述

typedef是用来申请类型别名的,本质是为数据类型起了一个别名,也相当于定义了一个新的类型。

②.定义数据类型

#include "iostream" using namespace std; typedef int * IntPtr; int main() { IntPtr x = new int(6); cout return a + b; } int sub(int a, int b) { return a - b; } int main() { func * calFunc; calFunc = add; cout return a + b; }

⑥.定义函数指针

#include "iostream" using namespace std; typedef int (*func)(int a,int b); int add(int a, int b) { return a + b; } int main() { func calFunc = add; cout typedef map type; }; strMap::type m_map; using

①.概述

c++11中通过 using来定义别名,比typedef更容易阅读

②.定义类型别名

using intPtr = int *; intPtr x = new int(6);

③.定义函数指针别名

#include "iostream" using namespace std; using func = int(*)(int a, int b); int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int main() { func calFunc = add; cout return a - b; } int main() { func *calFunc = add; cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有